import React from 'react';
import { Button, message } from 'antd';
import Modal from '../../../../components/Modal';
import CommonList from '../../../../components/CommonLists/list';
import InterfaceRelease from '../components/interfaceRelease';
import services from '../../../../services/api';

class InterfaceManageList extends CommonList {
  constructor(props) {
    super(props);
    this.api = 'interfaceManage';
    this.btnGroup = [
      (record) => (
        <Button type='link' onClick={() => this.handleDetail(record)}>
          管理
        </Button>
      ),
      (record) =>
        record.status ? (
          <Button type='link' onClick={() => this.handleOffline(record)}>
            下线
          </Button>
        ) : (
          <Button type='link' onClick={() => this.handleOnline(record)}>
            发布
          </Button>
        ),
      (record) => (
        <Button type='link' onClick={() => this.delete(record)}>
          删除
        </Button>
      ),
    ];
  }

  handleDetail({ id }) {
    this.props.history.push(`/dataGateway/interfaceManage/${id}/detail`);
  }

  handleOffline({ name, id }) {
    Modal.open({
      title: '',
      width: '360px',
      isConfirm: true,
      content: (
        <>
          <p style={{ marginBottom: '10px' }}>将下线接口: {name}</p>
          <p>该操作将导致此接口指定的环境无法被访问，可能会影响用户，请确保已经告知用户，或者确认需要强制下线?</p>
        </>
      ),
      isOkBtn: true,
      isCancelBtn: true,
      ok: () => {
        services[this.api].onOff({ id, onOrOff: 0 }).then(({ code, msg }) => {
          if (code === 0) {
            message.success(msg);
            this.props.queryList();
            Modal.close();
          } else {
            message.error(msg);
          }
        });
      },
      cancel: () => {
        Modal.close();
      },
    });
  }

  handleOnline(record) {
    Modal.open({
      title: '发布接口',
      width: '600px',
      content: <InterfaceRelease value={record} closeModal={() => Modal.close()} queryList={this.props.queryList} />,
      isOkBtn: false,
      isCancelBtn: false,
    });
  }
}

export default InterfaceManageList;
